<template>
  <div v-if="visible" class="modal-overlay" @click="handleOverlayClick">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>{{ title }}</h3>
        <button class="close-btn" @click="handleClose">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <line x1="18" y1="6" x2="6" y2="18"></line>
            <line x1="6" y1="6" x2="18" y2="18"></line>
          </svg>
        </button>
      </div>
      
      <div class="modal-body">
        <div class="form-group">
          <label for="process-select">选择流程配置</label>
          <select 
            id="process-select" 
            v-model="selectedProcessId" 
            class="form-control" 
            @change="handleProcessChange"
          >
            <option value="">请选择流程</option>
            <option v-for="process in processes" :key="process.id" :value="process.id">
              {{ process.name }}
            </option>
          </select>
        </div>
        
        <div v-if="selectedProcess" class="process-detail">
          <h4>流程详情</h4>
          <p><strong>描述：</strong>{{ selectedProcess.description || '无描述' }}</p>
          <p><strong>状态：</strong>{{ selectedProcess.status === 1 ? '启用' : '禁用' }}</p>
        </div>
      </div>
      
      <div class="modal-footer">
        <button class="btn btn-secondary" @click="handleClose">取消</button>
        <button 
          class="btn btn-primary" 
          @click="handleConfirm" 
          :disabled="!selectedProcessId"
        >
          确认
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, watch } from 'vue';
import { getProcessConfigs } from '../services/processConfigService';

export default {
  name: 'ProcessSelectModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '选择流程'
    },
    dataSourceId: {
      type: String,
      default: ''
    },
    dataSourceName: {
      type: String,
      default: ''
    }
  },
  emits: ['close', 'confirm', 'showMessage'],
  setup(props, { emit }) {
    const processes = ref([]);
    const selectedProcessId = ref('');
    const selectedProcess = ref(null);
    const loading = ref(false);
    
    // 格式化日期
    const formatDate = (dateString) => {
      if (!dateString) return '';
      const date = new Date(dateString);
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    };
    
    // 获取流程列表
    const fetchProcesses = async () => {
      loading.value = true;
      try {
        // 使用pageSize为100获取所有流程，实际项目中可能需要分页处理
        const response = await getProcessConfigs({ page: 0, pageSize: 100 });
        
        // 假设接口返回格式为 { data: { list: [] } } 或者 { data: [] }
        if (response && response.data) {
          if (Array.isArray(response.data)) {
            processes.value = response.data;
          } else if (response.data.list) {
            processes.value = response.data.list;
          } else {
            processes.value = [];
          }
        }
      } catch (error) {
        console.error('获取流程配置失败:', error);
        // 使用emit通知父组件显示提示
        emit('showMessage', '获取流程配置失败，请重试', '错误', 'error');
      } finally {
        loading.value = false;
      }
    };
    
    // 处理流程选择变化
    const handleProcessChange = () => {
      if (selectedProcessId.value) {
        selectedProcess.value = processes.value.find(p => p.id === selectedProcessId.value);
      } else {
        selectedProcess.value = null;
      }
    };
    
    // 处理关闭
    const handleClose = () => {
      selectedProcessId.value = '';
      selectedProcess.value = null;
      emit('close');
    };
    
    // 处理确认
    const handleConfirm = () => {
      if (selectedProcessId.value && selectedProcess.value) {
        emit('confirm', {
          processId: selectedProcessId.value,
          processName: selectedProcess.value.name,
          dataSourceId: props.dataSourceId,
          dataSourceName: props.dataSourceName
        });
        handleClose();
      }
    };
    
    // 处理点击遮罩层
    const handleOverlayClick = () => {
      handleClose();
    };
    
    // 监听visible变化，当显示时获取流程列表
    watch(() => props.visible, (newVal) => {
      if (newVal) {
        fetchProcesses();
      }
    });
    
    return {
      processes,
      selectedProcessId,
      selectedProcess,
      loading,
      formatDate,
      handleProcessChange,
      handleClose,
      handleConfirm,
      handleOverlayClick
    };
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e8e8e8;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #999;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background-color: #f5f5f5;
  color: #666;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-control {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-control:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.process-detail {
  margin-top: 20px;
  padding: 16px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.process-detail h4 {
  margin: 0 0 12px 0;
  font-size: 16px;
  color: #333;
}

.process-detail p {
  margin: 8px 0;
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid #e8e8e8;
}

.btn {
  padding: 8px 16px;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #545b62;
}

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background-color: #0056b3;
}

.btn-primary:disabled {
  background-color: #6c757d;
  cursor: not-allowed;
  opacity: 0.6;
}

@media (max-width: 768px) {
  .modal-content {
    width: 95%;
    margin: 20px;
  }
}
</style>